package com.biosimilarity.parallelOgram.snippet

// import scala.xml.NodeSeq
// import net.liftweb.http.S._
// import net.liftweb.util.Helpers._
// import net.liftweb.http.js.{JsCmd, JsCmds}

import net.liftweb._
import http._
import S._
import SHtml._
import util._
import Helpers._
import mapper._
import textile._
import js.{JE, JsCmd, JsCmds}
import JsCmds._
import JE._

import com.biosimilarity.parallelOgram.model.REPL

//import scala.xml.{NodeSeq, Text, MetaData, Null}
import scala.xml._

class REPLForm {
  val theREPL = new REPL()
  var theTerm : String = "0"
  var theClientRequestStr : String = evalStr()

  def evalStr() = theTerm
  def clientRequestRequest() = theClientRequestStr

  def parseTreeNode(clientRequestStr : String) = {
    theClientRequestStr = clientRequestStr.replace( "\n", "" );
    var theParseResponseStr : String =
      ( "failure: " + theClientRequestStr + "\n" );
    try {
      theParseResponseStr =
	theREPL.showClientRequestParseTree(theClientRequestStr)
	//theREPL.readEvalPrint(theClientRequestStr)
    }
    catch {
      case e => {
	val sw : java.io.StringWriter =	new java.io.StringWriter( );
	e.printStackTrace( new java.io.PrintWriter( sw, true ) );
	theParseResponseStr = theParseResponseStr + e.toString
      }
    }
    <div id="parseTree">{theParseResponseStr}</div>
  }
  
  def whatNode(termStr : String) = {
    theTerm = termStr
    theClientRequestStr = evalStr()
    var theParseResponseStr : String =
      ("failure: " + theClientRequestStr + "\n");
    try {
      theParseResponseStr =
	theREPL.showClientRequestParseTree(theClientRequestStr)
	//theREPL.readEvalPrint(theClientRequestStr)
    }
    catch {
      case e => {
	val sw : java.io.StringWriter =	new java.io.StringWriter( );
	e.printStackTrace( new java.io.PrintWriter( sw, true ) );
	theParseResponseStr = theParseResponseStr + e.toString
      }
    }
    <div id="parseTree">{theParseResponseStr}</div>
  }

  def updateWhat(str: String): JsCmd = {
    println("updateWhat with " + str)
    JsCmds.SetHtml("parseTree", whatNode(str))
  }

  def show(xhtml: NodeSeq): NodeSeq = {
    <xml:group>            
      <label for="whatField">Term :</label>
      { text("0", (updateWhat _)) % ("size" -> "60") % ("id" -> "whatField") }
      { <button type="button">{?("Go")}</button> % 
       ("onclick" -> ajaxCall(JsRaw("$('#whatField').attr('value')"), s => updateWhat(s))) }
      <br/>      
      <div id="parseTree">{whatNode(evalStr())}</div>
    </xml:group>
  }
}
